package cloud.dandanit.itservice.controller.video;


import cloud.dandanit.itservice.entity.Article;
import cloud.dandanit.itservice.entity.Video;
import cloud.dandanit.itservice.service.VideoService;
import cloud.dandanit.itservice.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcloud.vod.model.VodUploadResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author luokai
 * @since 2022-07-04
 */
@SuppressWarnings({"DuplicatedCode"})
@RestController
@RequestMapping("/itservice/videoadmin")
public class VideoAdminController {


    @Autowired
    private VideoService videoService;


    /**
     * 上传视频到腾讯云
     * @param file 文件
     * @return 成功
     */
    @ApiOperation("上传视频到腾讯云点播接口")
    @PostMapping("uploadTencentVideo")
    public R uploadTencentVideo(
            @ApiParam(name = "file", value = "前端文件", required = true)
            MultipartFile file) {

        VodUploadResponse response = videoService.uploadVideo(file);
        return R.ok();
    }

    /**
     * 生成签名
     * @return 签名
     */
    @ApiOperation("生成腾讯云点播签名接口")
    @GetMapping("getSign")
    public R getUploadSign(){

        String signature = videoService.getUploadSign();
        return R.ok().data("signature", signature);
    }


    /**
     * 删除视频
     * @param fileId 视频id
     * @return 成功
     */
    @ApiOperation("删除单个腾讯云点播视频接口")
    @DeleteMapping("removeTencentVideo/{fileId}")
    public R removeTencentVideoByFileId(@PathVariable String fileId) {

        videoService.removeTencentVideoByFileId(fileId);
        return R.ok();
    }

    /**
     * 删除视频
     * @param videoId 视频id
     * @return 成功
     */
    @ApiOperation("删除视频数据库接口")
    @DeleteMapping("deleteVideoById/{videoId}")
    public R deleteVideoById(@PathVariable String videoId) {

        videoService.removeById(videoId);
        return R.ok();
    }

    /**
     * 删除章节或者课程下面的多个视频
     * @param videoList 视频列表
     * @return 成功
     */
    @ApiOperation("批量删除腾讯云点播视频接口")
    @DeleteMapping("delete-batch")
    public R removeTencentVideoByFileIds(@RequestParam("videoList") List<String> videoList) {

        videoService.removeTencentVideoByFileIds(videoList);
        return R.ok();
    }

    /**
     * 获取视频
     * @return 视频
     */
    @ApiOperation(value = "获取视频分页列表")
    @GetMapping("pageVideo/{page}/{limit}")
    public R getPageVideo(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Video> pageParam = new Page<>(page, limit);
        videoService.page(pageParam,null);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    /**
     * 修改视频封面
     * @param video 视频
     * @return 成功
     */
    @ApiOperation(value = "修改视频封面")
    @PostMapping("updateVideoCover")
    public R updateVideoCover(
            @ApiParam(name = "video", value = "视频实体", required = true)
            @RequestBody Video video) {

        videoService.updateById(video);
        return R.ok();
    }

    /**
     * 根据视频id获取视频信息
     * @param videoId 视频id
     * @return 视频信息
     */
    @ApiOperation(value = "根据视频id获取视频信息接口")
    @GetMapping("getVideoById/{videoId}")
    public R getVideoInfoById(
            @ApiParam(name = "videoId", value = "视频ID", required = true)
            @PathVariable String videoId) {

        Video video = videoService.getById(videoId);
        return R.ok().data("video",video);
    }

    /**
     * 修改视频
     * @param video 视频
     * @return 成功
     */
    @ApiOperation(value = "修改视频")
    @PostMapping("updateVideo")
    public R updateVideo(
            @ApiParam(name = "video", value = "视频实体", required = true)
            @RequestBody Video video) {

        videoService.updateById(video);
        return R.ok();
    }

    /**
     * 增加视频
     * @param video 视频
     * @return 成功
     */
    @ApiOperation(value = "增加视频")
    @PostMapping("addVideo")
    public R addVideo(
            @ApiParam(name = "video", value = "视频实体", required = true)
            @RequestBody Video video) {

        videoService.save(video);
        return R.ok();
    }

    @ApiOperation(value = "更改视频状态")
    @GetMapping("changeState/{videoId}")
    public R changeState(
            @ApiParam(name = "videoId", value = "视频ID", required = true)
            @PathVariable String videoId) {

        Video video = videoService.getById(videoId);
        if (video.getState()==0) {
            video.setState(1);
        } else {
            video.setState(0);
        }
        videoService.updateById(video);
        return R.ok();
    }

    @ApiOperation(value = "一键修改视频状态")
    @GetMapping("batchChangeVideoState/{state}")
    public R batchChangeVideoState(
            @ApiParam(name = "state", value = "更改的状态", required = true)
            @PathVariable Integer state) {

        List<Video> videoList = videoService.list(null);
        for (Video video : videoList) {
            video.setState(state);
        }
        videoService.updateBatchById(videoList);
        return R.ok();
    }
}

